package main

import (
	"fmt"
	"github.com/go-redis/redis"
	"strconv"
	"time"
)

var cli *redis.Client

func main() {
	client := redis.NewClient(&redis.Options{
		Addr: "192.168.70.132:6379",
		// Password: "zack@root123.com", // no password set
		DB: 4, // use default DB
	})
	defer func() {
		err := client.Close()
		if err != nil {
			fmt.Println("close client err: ", err)
		}
	}()
	pong, err := client.Ping().Result()
	fmt.Println(pong, err)
	// TestRedis(client)
	cli = client
	Test12()
}

func Test12() {
	//cmd := cli.Set("msg", "hahao", 100)
	//fmt.Println("保存结果", cmd.Err())
	//TestHashExpire()
	TestStringExpire()
}

func TestRedis(cli *redis.Client) {
	RdsSetPayUserSessionByHash(190051, 3193000006, "payGate.10.event.bqmh1m5saemaqhm2kmjg:190051:3193000006:2:ThXyBZRdDvNf3IegQholQG7oSpVPzAVhn07%2B4ftYTtqSyBgVQw45hKYiUH4FKZhs", 1599914905)
}

func RdsSetPayUserSessionByHash(platID uint32, uid uint32, field string, val int64) error {
	strPlat := strconv.FormatUint(uint64(platID), 10)
	strUid := strconv.FormatUint(uint64(uid), 10)
	key := "PAYMENT:SESSION:USER:" + strPlat + ":" + strUid
	ret := cli.HSet(key, field, val)
	if ret.Err() != nil {
		return ret.Err()
	}
	return nil
}

func TestHashExpire() {
	//key := "HASH_TEST"
	// cli.HSet(key,"hello", 1234)
	// cli.Expire(key, time.Second*time.Duration(5))
}

func TestStringExpire() {
	key := "STRING_TEST"
	cli.Set(key, "hello", 0)
	cli.Expire(key, time.Second*time.Duration(10))
}
